import { BodySetup } from "utils/creep/CreepSetup";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { role_crossLevel } from "utils/constants/moveConstant";
import { priority } from "utils/constants/definetypes";
import { EActionCode } from "framework/FSM/Action/ActionCode";
import { object } from "lodash";


/**
 * 升级Action
 */
/*@profile*/
export class ActionWithdraw  extends ActionBaseOfCreep<IActionCMD<"withdraw">>
{
    protected updateArgs(action: IActionCMD<"withdraw">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        action.arg.store = Game.getObjectById(action.arg.store.id) as AnyStoreStructure;
        if(action.arg.resource)
        {
            if(action.arg.resource.length ==0)
            {
                action.arg.entity.log('info',`资源已经处理完`);
                return EActionCode.FAILED;
            }
        }
        //     if(action.arg.resource.length ==1)
        //     {
        //         if(!action.arg.resource[0].amount)
        //         {
        //             if(action.arg.entity.store.getUsedCapacity() >0)
        //             {
        //                 return EActionCode.FAILED;
        //             }
        //         }
        //     }
        // }
        // else 
        // {
        //     if(action.arg.entity.store.getUsedCapacity() >0)
        //     {
        //         return EActionCode.FAILED;
        //     }
        // }
        if(action.arg.entity.store.getFreeCapacity()==0)
        {
            // 爬装满了
            return EActionCode.FAILED;
        }
        
        if(!action.arg.store)
        {
            action.arg.entity.log('info',`仓库不存在`);
            // 仓库不存在
            return EActionCode.FAILED;
        }
        if((action.arg.store as AnyStoreStructure).store.getUsedCapacity() ==0)
        {
            action.arg.entity.log('info',`仓库没有资源`);
            // 仓库没有资源
            return EActionCode.FAILED;
        }
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"withdraw">): ActionCode 
    {
       
        const structure = action.arg.store;
        const creep = action.arg.entity;
  
        if(structure.pos.distance(creep.pos)>1)
        {
            creep.memory.crossLevel = priority.A;
            creep.mymoveTo({pos:structure.pos,range:1,rangelimit:3},{});
            creep.memory.crossLevel =   role_crossLevel[creep.memory.Role];
        }
        else
        {
            if(action.arg.resource)
            {
               // global.Log.Info(`ActionWithdraw: 取资源:${creep.name} 1`);
                // 取指定资源
                for(let i=0;i<action.arg.resource.length;i++)
                {
                    const e=action.arg.resource[i];
                    const exist = structure.store.getUsedCapacity(e.resourceType);
                    if(!exist)
                    {
                        // 没有我要得资源
                        return EActionCode.OK;
                        // continue;
                    }
                    if(e.amount)
                    {
                        creep.withdraw(structure,e.resourceType,e.amount);
                        action.arg.resource.splice(i,1);
                        return EActionCode.RUNNING;
                    }
                    else
                    {
                        const code = creep.withdraw(structure,e.resourceType);
                        action.arg.resource.splice(i,1);
                        return EActionCode.RUNNING;
                        // global.Log.Info(` code :${code}  ActionWithdraw: ${e.resourceType}取资源:${creep.name}  ${exist} - ${free}`);
                    }
                }
            }
            else
            {
                // 全部取出来
                for(const k in structure.store)
                {
                    creep.withdraw(structure,k as ResourceConstant );
                    break;
                }
                if(Object.keys(structure.store).length==1)
                {
                    return EActionCode.OK;
                }
            }
            
        }
        return EActionCode.RUNNING;
    }
}